/*
    This file is part of Titan.

    Titan is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of 
    the License, or (at your option) any later version.

    Titan is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Titan. If not, see <http://www.gnu.org/licenses/>.
*/

package titancommon.applications;


import java.awt.Graphics;
import java.awt.Image;

/*
 * ShowGraphics.java
 *
 * Created on 5. August 2008, 13:59
 */
/**
 *
 * @author  Mirco Rossi <rossi@ife.ee.ethz.ch>
 */
public class FarkleFrame extends java.awt.Frame {

    /** Display states */
    public static final int DICE_NEUTRAL  = 0;
    public static final int DICE_PICKEDUP = 1;
    public static final int DICE_BLOCKED  = 2;
    
    Image m_img[];
    int m_scores[];
    int m_states[];

    String m_PicFolder;
    
    public class NextPlayerMsg {
      
    }

    /** Creates new form ShowGraphics */
    public FarkleFrame() {

        initComponents();
        m_img=new Image[6];
        m_scores=new int[6];
        m_states=new int[6];
             
        m_PicFolder="pics/";  
        for(int i=0;i<6;i++){
            setDice(i+1,0,0);
            m_scores[i]=0;
            m_states[i]=0;
        }        
        
    }

    /**
     * Sets the images on the frame to the current dice state
     * @param dice Dice ID to be set values (0-5)
     * @param eyes Number of eyes to show (1-6)
     * @param state Dice state. Can be one of DICE_NEUTRAL, DICE_PICKEDUP, DICE_BLOCKED
     * @return Whether the dice could be set to the given state
     */
    public boolean setDice(int dice, int eyes, int state) {
        
        if (dice >= m_img.length) return false;
        
        String stateName="_white_";
        switch(state){
                case 0:
                    stateName="_white_"; break;
                case 1:
                    stateName="_blue_"; break;
                case 2:
                    stateName="_red_";break;
            default:
                return false;
        }
                         
        String sFile;
        
        if (state == DICE_PICKEDUP) sFile = m_PicFolder +"dice_pickup.gif";
        else sFile = m_PicFolder +"dice" + stateName+ Integer.toString(eyes)+".gif";
        m_img[dice] = getToolkit().getImage(sFile);
        updateCanvas(dice);
        
        m_scores[dice]=eyes;
        m_states[dice]=state;
        
        return true;
    }
    
    public void setMessage(String strMessage) {
        playerMessage.setText(strMessage);
    }
    
    public boolean setScore(int iPlayer, int iScore, int iTotalScore) {
       boolean bResult = true;
       switch(iPlayer) {
          case 0: curScore1.setText(""+iScore);
                  totalScore1.setText(""+iTotalScore);
                  break;
          case 1: curScore2.setText(""+iScore);
                  totalScore2.setText(""+iTotalScore);
          default:
             bResult = false;
       }
       return bResult;
    }
    

    private void updateCanvas(int dice){
        switch(dice){
            case 0: canvas1.repaint();break;
            case 1: canvas2.repaint();break;
            case 2: canvas3.repaint();break;
            case 3: canvas4.repaint();break;
            case 4: canvas5.repaint();break;
            case 5: canvas6.repaint();break;
        }
    }
            
            
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      canvas1 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[0], 0, 0, this );
         }

      };
      Fix1Label = new java.awt.Label();
      Fix2Label = new java.awt.Label();
      curScore1 = new java.awt.Label();
      canvas2 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[1], 0, 0, this );
         }

      };
      canvas3 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[2], 0, 0, this );
         }

      };
      canvas4 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[3], 0, 0, this );
         }

      };
      canvas5 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[4], 0, 0, this );
         }

      };
      canvas6 = new java.awt.Canvas() {
         public void paint( Graphics g )
         {
            g.drawImage( m_img[5], 0, 0, this );
         }

      };
      Fix2Label1 = new java.awt.Label();
      curScore2 = new java.awt.Label();
      playerMessage = new java.awt.Label();
      totalScore1 = new java.awt.Label();
      totalScore2 = new java.awt.Label();
      buttonCloseWindow = new java.awt.Button();
      buttonNextPlayer = new java.awt.Button();

      setResizable(false);
      addWindowListener(new java.awt.event.WindowAdapter() {
         public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm(evt);
         }
      });

      Fix1Label.setAlignment(java.awt.Label.CENTER);
      Fix1Label.setBackground(new java.awt.Color(51, 102, 255));
      Fix1Label.setFont(new java.awt.Font("Arial", 1, 20));
      Fix1Label.setForeground(new java.awt.Color(255, 255, 255));
      Fix1Label.setText("Pervasive Farkle");

      Fix2Label.setText("Player 1:");

      curScore1.setAlignment(java.awt.Label.RIGHT);
      curScore1.setName("curScore1"); // NOI18N
      curScore1.setText("0");

      Fix2Label1.setText("Player 2:");

      curScore2.setAlignment(java.awt.Label.RIGHT);
      curScore2.setName("curScore2"); // NOI18N
      curScore2.setText("0");

      playerMessage.setAlignment(java.awt.Label.CENTER);
      playerMessage.setName("Message"); // NOI18N
      playerMessage.setText("Initializing Farkle game");

      totalScore1.setAlignment(java.awt.Label.RIGHT);
      totalScore1.setName("totalScore1"); // NOI18N
      totalScore1.setText("0");

      totalScore2.setAlignment(java.awt.Label.RIGHT);
      totalScore2.setName("totalScore2"); // NOI18N
      totalScore2.setText("0");

      buttonCloseWindow.setLabel("Close window");
      buttonCloseWindow.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            closeButonActionPerformed(evt);
         }
      });

      buttonNextPlayer.setActionCommand("Next Player");
      buttonNextPlayer.setLabel("Next Player");
      buttonNextPlayer.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            buttonNextPlayercloseButonActionPerformed(evt);
         }
      });

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(org.jdesktop.layout.GroupLayout.TRAILING, Fix1Label, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
         .add(layout.createSequentialGroup()
            .add(33, 33, 33)
            .add(Fix2Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(156, 156, 156))
         .add(layout.createSequentialGroup()
            .add(33, 33, 33)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
               .add(Fix2Label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
               .add(curScore1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
               .add(curScore2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
               .add(canvas2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
               .add(totalScore1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
               .add(totalScore2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
               .add(canvas3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
               .add(canvas6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
            .addContainerGap(36, Short.MAX_VALUE))
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(buttonNextPlayer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(buttonCloseWindow, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
            .addContainerGap())
         .add(layout.createSequentialGroup()
            .add(10, 10, 10)
            .add(playerMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
            .addContainerGap(10, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .add(Fix1Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(Fix2Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(totalScore1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(curScore1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(Fix2Label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(totalScore2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(curScore2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(canvas1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(canvas4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(canvas6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(playerMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
               .add(buttonCloseWindow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(buttonNextPlayer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

private void closeButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButonActionPerformed
   this.setVisible(false);
}//GEN-LAST:event_closeButonActionPerformed

private void buttonNextPlayercloseButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNextPlayercloseButonActionPerformed
  titancommon.TitanCommand.singleton.m_msgDispatcher.sendMessage(new NextPlayerMsg());
}//GEN-LAST:event_buttonNextPlayercloseButonActionPerformed
  


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FarkleFrame ff = new FarkleFrame();
                ff.setDice(0, 1, DICE_NEUTRAL);
                ff.setDice(1, 5, DICE_NEUTRAL);
                ff.setDice(2, 5, DICE_NEUTRAL);
                ff.setDice(3, 5, DICE_NEUTRAL);
                ff.setDice(4, 1, DICE_NEUTRAL);
                ff.setDice(5, 2, DICE_NEUTRAL);
                ff.setDice(1, 4, DICE_PICKEDUP);
                ff.setDice(5, 2, DICE_PICKEDUP);
                ff.setScore(0, 300, 5600);
                ff.setScore(1, 0, 6150);
                ff.setVisible(true);
                
            }
        });
    }


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private java.awt.Label Fix1Label;
   private java.awt.Label Fix2Label;
   private java.awt.Label Fix2Label1;
   private java.awt.Button buttonCloseWindow;
   private java.awt.Button buttonNextPlayer;
   private java.awt.Canvas canvas1;
   private java.awt.Canvas canvas2;
   private java.awt.Canvas canvas3;
   private java.awt.Canvas canvas4;
   private java.awt.Canvas canvas5;
   private java.awt.Canvas canvas6;
   private java.awt.Label curScore1;
   private java.awt.Label curScore2;
   private java.awt.Label playerMessage;
   private java.awt.Label totalScore1;
   private java.awt.Label totalScore2;
   // End of variables declaration//GEN-END:variables

}
